package com.atu.atugra.controller;
 
 
 
import com.atu.atugra.constant.AppHttpCodeEnum;
import com.atu.atugra.entity.Categories;
import com.atu.atugra.service.CategoriesService;
import com.atu.atugra.tools.CommonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 分类表(Categories)表控制层
 *
 * @author makejava
 * @since 2025-04-18 23:08:27
 */
@RestController
@RequestMapping("category")
public class CategoriesController{
    @Autowired
    private CategoriesService categoriesService;
    //分页查询
    @GetMapping("/list")
    public CommonResult list(@RequestParam Integer currentPage,@RequestParam Integer pageSize,@RequestParam(required = false) String key){
        return categoriesService.Categorylist(currentPage,pageSize,key);
    }
    //新增
    @PostMapping("/add")
    public CommonResult add(@RequestBody Categories categories){
        return CommonResult.okResult(categoriesService.save(categories));
    }
    //修改
    @PostMapping("/update")
    public CommonResult update(@RequestBody Categories categories){
        return CommonResult.okResult(categoriesService.updateById(categories));
    }
    //删除
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestParam Integer id){
        return CommonResult.okResult(categoriesService.removeById(id));
    }
    // 批量删除
    @DeleteMapping("/deleteBatch")
    public CommonResult deleteBatch(@RequestBody List<Integer> ids){
        return CommonResult.okResult(categoriesService.removeByIds(ids));
    }
    // 新增获取所有分类方法
    @GetMapping("/all")
    public CommonResult getAllCategories() {
        return categoriesService.all();
    }
    @GetMapping("/getById/{categoryId}")
    public CommonResult getById(@PathVariable Long categoryId) {
        Categories category = categoriesService.getById(categoryId);
        if (category != null) {
            return CommonResult.okResult(category);
        }
        return CommonResult.failed(AppHttpCodeEnum.ERROR);
    }
}
